//
//  BMAStatsViewController.m
//  Autismus
//
//  Created by Nicolas CHENG on 15/01/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BMAStatsViewController.h"
#import "BMAScoreProgressionBarView.h"

#import "BMAScoreAdvancement.h"
#import "BMAScoreAdvancementChartView.h"

#import "BMAStatsManager.h"
#import "BMAGame.h"
#import "BMAAnswer.h"

@implementation BMAStatsViewController

@synthesize progBarView;
@synthesize scoreAdvChartView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self != nil)
    {
        
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _scoreAdvDataSource = [[BMAScoreAdvancement alloc] init];
    _scoreAdvDataSource.date = [NSDate date];
    self.scoreAdvChartView.dataSource = _scoreAdvDataSource;

#if DEBUG
    NSArray *fetchResults = [BMAGame fetchEntities];
    DLOG(@"Fetch");
    for (BMAGame *game in fetchResults)
    {     
        DLOG(@"New Game %@ %@", [game.startDate description], [game.endDate description]);
        for (BMAAnswer *answer in game.answers) {
            DLOG(@"     Question :[%@] ; Start Time : [%@] ; End Time : [%@]", ((answer.value) ? (@"YES") : (@"NO")), [answer.startDate description], [answer.endDate description]);
        }
    }
#endif
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.progBarView = nil;
    self.scoreAdvChartView = nil;
}

-(IBAction)goBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return YES;
}

- (IBAction)testAction:(id)sender
{
    [UIView animateWithDuration:1.5 animations:^{
        CGRect rect = self.scoreAdvChartView.frame;
        rect.size.width = 980;
        self.scoreAdvChartView.frame = rect;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - BMAScoreProgBarView DataSource

- (NSUInteger)numberOfGameInProgBarView:(BMAScoreProgressionBarView *)progBarView
{
    return 3;
}

- (NSUInteger)progBarView:(BMAScoreProgressionBarView *)progBarView percentageValueForGameAtIndex:(NSInteger)index
{
    NSUInteger perc = [[BMAStatsManager sharedInstance] percentElapsedTimeForDate:[NSDate date] idGame:index];
    return perc;
}

- (UIColor *)progBarView:(BMAScoreProgressionBarView *)progBarView colorForGameAtIndex:(NSInteger)index
{
    UIColor *color = nil;
    
    switch (index) {
        case BMAIDGamePaires:
            color = [UIColor orangeColor];
            break;
        
        case BMAIDGameIntrus:
            color = [UIColor purpleColor];
            break;
    
        case BMAIDGameDevinettes:
            color = [UIColor yellowColor];
            break;
            
        default:
            color = [UIColor blueColor];
            break;
    }
    return color;
}

@end
